import 'package:flutter/material.dart';
import 'package:share_fit/app/controllers/device/remind/picker/model/date_mode.dart';
import 'package:share_fit/app/controllers/device/remind/picker/model/pduration.dart';
import 'package:share_fit/app/controllers/device/remind/picker/model/suffix.dart';
import 'package:share_fit/app/controllers/device/remind/picker/pickers.dart';

class DatePickerPage extends StatefulWidget {

  @override
  _DatePickerPageState createState() => _DatePickerPageState();
}

class _DatePickerPageState extends State<DatePickerPage> {
  String stateText = '';
  var selectData = {
    DateMode.YMDHMS: '',
    DateMode.YMDHM: '',
    DateMode.YMDH: '',
    DateMode.YMD: '',
    DateMode.YM: '',
    DateMode.Y: '',
    DateMode.MDHMS: '',
    DateMode.HMS: '',
    DateMode.MD: '',
    DateMode.S: '',
  };

  final divider = Divider(height: 1, indent: 20);
  final rightIcon = Icon(Icons.keyboard_arrow_right);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择器'),
      ),
      body: ListView(
        children: [
          _item('年月日', DateMode.YMD),
          _item('时分', DateMode.HM),
          SizedBox(height: 80)
        ],
      ),
    );
  }

  Widget _item(title, model) {
    return Column(
      children: [
        Container(
          color: Colors.white,
          child: ListTile(
            title: Text(title),
            onTap: () {
              _onClickItem(model);
            },
            trailing: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
              Text(selectData[model]==null?"":selectData[model]!),
              rightIcon
            ]),
          ),
        ),
        divider,
      ],
    );
  }

  void _onClickItem(model) {
    Pickers.showDatePicker(
      context,
      mode: model,
      suffix: Suffix.normal(),
      minDate: PDuration(year: 2020, month: 2, day: 10),
      maxDate: PDuration(second: 22),

      // selectDate: PDuration(hour: 18, minute: 36, second: 36),
      // minDate: PDuration(hour: 12, minute: 38, second: 3),
      // maxDate: PDuration(hour: 12, minute: 40, second: 36),
      onConfirm: (p) {
        print('longer >>> 返回数据：$p');
        setState(() {
          switch (model) {
            case DateMode.YMDHMS:
              selectData[model] =
              '${p.year}-${p.month}-${p.day} ${p.hour}:${p.minute}:${p.second}';
              break;
            case DateMode.YMDHM:
              selectData[model] =
              '${p.year}-${p.month}-${p.day} ${p.hour}:${p.minute}';
              break;
            case DateMode.YMDH:
              selectData[model] = '${p.year}-${p.month}-${p.day} ${p.hour}';
              break;
            case DateMode.YMD:
              selectData[model] = '${p.year}-${p.month}-${p.day}';
              break;
            case DateMode.YM:
              selectData[model] = '${p.year}-${p.month}';
              break;
            case DateMode.Y:
              selectData[model] = '${p.year}-${p.month}';
              break;
            case DateMode.MDHMS:
              selectData[model] =
              '${p.month}-${p.day} ${p.hour}:${p.minute}:${p.second}';
              break;
            case DateMode.HMS:
              selectData[model] = '${p.hour}:${p.minute}:${p.second}';
              break;
            case DateMode.MD:
              selectData[model] = '${p.month}-${p.day}';
              break;
            case DateMode.S:
              selectData[model] = '${p.second}';
              break;
            case DateMode.HM:
              selectData[model] = '${p.hour}:${p.minute}';
              break;
          }
        });
      },
      // onChanged: (p) => print(p),
    );
  }

}